#!/usr/bin/python

print "Content-Type: text/xml\n\n"
#===============================================================================
# print "Content-type: text/html"
# print
# print "<html><head><title>Situation snapshot</title></head>"
# print "<body><pre>"
# 
# import sys
# sys.stderr = sys.stdout
# import os
# from cgi import escape
# 
# print "<strong>Python %s</strong>" % sys.version
# 
# for k in sorted(os.environ.keys()):
#    print "%s\t%s" % (escape(k), escape(os.environ[k]))
# 
# print "</pre></body></html>"
#===============================================================================
# TODO: remove these
import cgitb
#cgitb.enable(display=0, logdir="/tmp")
cgitb.enable()

import os, sys
sys.stderr = sys.stdout
sys.path.insert(0, __file__.split("\\GMT\\")[0])
#sys.path.append(os.path.split(os.path.split(os.path.split(__file__)[0])[0])[0])

from GMT.messaging.xmlpickle import pickle, unpickle
from GMT.messaging.requests import RootObject

# TODO:remove this test method 
def test():
    x = RootObject()
    xml = pickle(x)
    x = unpickle(xml)
    print x
    #open("test.xml", "wt").write(pickle(x))
    exit(0)
    
if __name__=="__main__":
    
    # TODO: add some post request verification code
    #print os.environ["CONTENT_TYPE"]
    if os.environ.has_key("CONTENT_LENGTH") and int(os.environ["CONTENT_LENGTH"]) > 0:
        xml = sys.stdin.read(int(os.environ["CONTENT_LENGTH"]))
    else:
        print "invalid request"
        sys.exit(1)
    
    x = unpickle(xml)
    x.counter = 4
    
    print pickle(x)